package com.yanqu.road.server.cmd.activity.twins.cross;

import com.yanqu.road.entity.activity.twins.config.TwinsConfig;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.TwinsProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.twins.TwinsModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.twins.TwinsMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = GameProtocol.S_TWINS_CROSS_LUCK_DRAW, desc = "")
public class CrossTwinsLuckDrawCmd extends AbstractCommand {

    TwinsProto.TwinsLuckDrawRespMsg reqMsg;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = TwinsProto.TwinsLuckDrawRespMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        TwinsConfig config = TwinsMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        int checkGroup = player.getModule(TwinsModule.class).checkGroup();
        if (checkGroup != 0) {
            ret = checkGroup;
            return;
        }
        if(reqMsg.getTimes() > 0){
            int count = reqMsg.getTimes();
            Property cost = new Property();
            for(int i = 0; i < count ; i++){
                cost.addProperty(PropertyHelper.parseStringToProperty(config.getLuckDrawCost()));
            }
            player.getModule(PreReduceModule.class).restorePreReduce(cost);
            if(ret == 0){
                player.getModule(CurrencyModule.class).removeCurrency(cost, eLogMoneyType.Twins,eLogMoneyType.TwinsSkinLuckDrawCost);
            }
        }

        if(!StringUtils.isNullOrEmpty(reqMsg.getReward()) && reqMsg.getTimes() > 0){
            player.getModule(CurrencyModule.class).addCurrency(PropertyHelper.parseStringToProperty(reqMsg.getReward()), eLogMoneyType.Twins,eLogMoneyType.TwinsSkinLuckDrawReward);
        }


        player.sendPacket(ClientProtocol.U_TWINS_LUCK_DRAW, reqMsg.toBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret == 0) {
            return;
        }
    }
}
